<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>地理距离聚合 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/geo-distance-agg.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/geo-distance-agg.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/geo-distance-agg.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/geo-distance-agg.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="geoloc.html">地理位置</a></span>
»
<span class="breadcrumb-link"><a href="geo-aggs.html">地理位置聚合</a></span>
»
<span class="breadcrumb-node">地理距离聚合</span>
</div>
<div class="navheader">
<span class="prev">
<a href="geo-aggs.html">« 地理位置聚合</a>
</span>
<span class="next">
<a href="geohash-grid-agg.html">Geohash 网格聚合 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="geo-distance-agg"></a>地理距离聚合<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/330_Geo_aggs/62_Geo_distance_agg.asciidoc">edit</a>
</h2>
</div></div></div>
<p><code class="literal">geo_distance</code> 聚合 对一些搜索非常有用，例如找到所有距离我 1km 以内的披萨店。搜索结果应该也的确被限制在用户指定 1km 范围内，但是我们可以添加在 2km 范围内找到的其他结果：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">GET /attractions/restaurant/_search
{
  "query": {
    "bool": {
      "must": {
        "match": { <a id="CO238-1"></a><i class="conum" data-value="1"></i>
          "name": "pizza"
        }
      },
      "filter": {
        "geo_bounding_box": {
          "location": { <a id="CO238-2"></a><i class="conum" data-value="2"></i>
            "top_left": {
              "lat":  40.8,
              "lon": -74.1
            },
            "bottom_right": {
              "lat":  40.4,
              "lon": -73.7
            }
          }
        }
      }
    }
  },
  "aggs": {
    "per_ring": {
      "geo_distance": { <a id="CO238-3"></a><i class="conum" data-value="3"></i>
        "field":    "location",
        "unit":     "km",
        "origin": {
          "lat":    40.712,
          "lon":   -73.988
        },
        "ranges": [
          { "from": 0, "to": 1 },
          { "from": 1, "to": 2 }
        ]
      }
    }
  },
  "post_filter": { <a id="CO238-4"></a><i class="conum" data-value="4"></i>
    "geo_distance": {
      "distance":   "1km",
      "location": {
        "lat":      40.712,
        "lon":     -73.988
      }
    }
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO238-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>主查询查找名称中含有 <code class="word">pizza</code> 的饭店。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO238-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p><code class="literal">geo_bounding_box</code> 筛选那些只在纽约区域的结果。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO238-3"><i class="conum" data-value="3"></i></a></p>
</td>
<td align="left" valign="top">
<p><code class="literal">geo_distance</code> 聚合统计距离用户 1km 以内，1km 到 2km 的结果的数量。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO238-4"><i class="conum" data-value="4"></i></a></p>
</td>
<td align="left" valign="top">
<p>最后，后置过滤器 <code class="literal">post_filter</code> 将结果缩小至那些在用户 1km 范围内的饭店。</p>
</td>
</tr>
</table>
</div>
<p>前面的请求  响应如下：</p>
<div class="pre_wrapper lang-json">
<pre class="programlisting prettyprint lang-json">"hits": {
  "total":     1,
  "max_score": 0.15342641,
  "hits": [ <a id="CO239-1"></a><i class="conum" data-value="1"></i>
     {
        "_index": "attractions",
        "_type":  "restaurant",
        "_id":    "3",
        "_score": 0.15342641,
        "_source": {
           "name": "Mini Munchies Pizza",
           "location": [
              -73.983,
              40.719
           ]
        }
     }
  ]
},
"aggregations": {
  "per_ring": { <a id="CO239-2"></a><i class="conum" data-value="2"></i>
     "buckets": [
        {
           "key":       "*-1.0",
           "from":      0,
           "to":        1,
           "doc_count": 1
        },
        {
           "key":       "1.0-2.0",
           "from":      1,
           "to":        2,
           "doc_count": 1
        }
     ]
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO239-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>后置过滤器 <code class="literal">post_filter</code> 已经将搜索结果缩小至仅在用户 1km 范围内的披萨店。</p>
</td>
</tr>
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO239-2"><i class="conum" data-value="2"></i></a></p>
</td>
<td align="left" valign="top">
<p>聚合包含了搜索结果，以及另外一家在用户 2km 范围内的披萨店。</p>
</td>
</tr>
</table>
</div>
<p>在这个例子中，我们计算了落在每个同心环内的饭店数量。当然，我们可以在 <code class="word">per_ring</code> 聚合下 嵌套子聚合来计算每个环的平均价格、最受欢迎程度等。</p>
<p style="color:#666;">
&gt;译者注: 本来只需要查询1km以内的数据, 但是在外面又套了一层1km~2km的数据, 所以就组成了同心环。
</p>

</div>
<div class="navfooter">
<span class="prev">
<a href="geo-aggs.html">« 地理位置聚合</a>
</span>
<span class="next">
<a href="geohash-grid-agg.html">Geohash 网格聚合 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>